var addOrUpdateNews = true;//true添加新闻false为修改
var checkedValue = new Array();

$().ready(function(){
	$("#openAddNews").click(function(){
		addOrUpdateNews = true;
		$("#btn_addOrUpdateNews").val("抢先发布");
		$("#idNews").val("");
		$("#titleNews").val("");
		$("#contentNews").val("");
		$(".addOrUpdateNews").show();
		
	});
	
	$("#closeAddNews").click(function(){
		$(".addOrUpdateNews").hide();
	});
	
	$("#btn_addOrUpdateNews").hover(function() {
		bg=$(this).css("background");
    	c=$(this).css("color"); 
        $(this).css({"background":"red","color":"#000"});
    }, function() {
        $(this).css({"background":bg,"color":c});
    }).click(function(){
    	var nId = $("#idNews").val();
    	var unit = $("#unitNews option:selected").val();
		var title = $("#titleNews").val();
		var content = $("#contentNews").val();
		var type=null;
		if(addOrUpdateNews==true){
			type = "add";
		}else{
			type = "update";
		}
		if(unit!="" && title!="" && content!="" && type!=null){
			$.ajax({
				"type":"post",
				"url":"../news/handleNews",
				"data":{
					"id":nId,
					"unit":unit,
					"title":title,
					"content":content,
					"type":type
				},
				"success":function(data){
					//不做处理    var data = eval("("+data+")"),因为在mvc-servlet配置文件中
			    	if (data.success == "成功") {
			    		window.location.reload();
			    	} else {
			    		$("#addOrUpdateFontTiShi").text("发布新闻失败...请重试或联系客服人员...").css({"color":"red"});
			    	}
				},
				"error":function(){
					$("#addOrUpdateFontTiShi").text("网络异常！请重试或联系管理员").css({"color":"red"});
				},
				"beforeSend":function(){
					$("#addOrUpdateFontTiShi").text("正在发布中！请稍等！");
				}
			});
		}
    });
	
	$("#selectAll").click(function(){
		if($("#selectAll").is(':checked')){
			$("input[name='delNewsArray[]']").attr("checked",'true');//全选   
		}else{
			$("input[name='delNewsArray[]']").removeAttr("checked");//取消全选   
		} 
	});
	
	  
});

//修改
function showUpdateNews(nId,unit,title,content){
	addOrUpdateNews = false;
	$("#btn_addOrUpdateNews").val("修改新闻");
	$("#idNews").val(nId);
	$("#unitNews option[value='"+unit+"']").attr("selected","selected"); 
	$("#titleNews").val(title);
	$("#contentNews").val(content);
	$(".addOrUpdateNews").show();
}
//删除选中新闻
function delNewsArray(){
	checkedValue = [];
	$('input[name="delNewsArray[]"]:checked').each(function(){
		checkedValue.push($(this).val());
	});
	if(checkedValue.length==0){
		alert("请选择您要删除的新闻！");
		return
	}
	var flag=confirm("您确认要删除选中的新闻吗");
	if(flag){
		$.ajax({
			"type":"post",
			"url":"../news/handleNews",
			"data":{
				"ids[]":checkedValue,
				"type":"delect"
			},
			dataType: "json",
			"success":function(data){
		    	if (data.success == "成功") {
		    		alert("删除成功！")
		    		$("#delectNewsTiShi").text("");
		    		window.location.reload();
		    	} else {
		    		$("#delectNewsTiShi").text("新闻删除失败...请重试或联系客服人员...").css({"color":"red"});
		    	}
			},
			"error":function(){
				$("#delectNewsTiShi").text("网络异常！请重试或联系管理员").css({"color":"red"});
			},
			"beforeSend":function(){
				$("#delectNewsTiShi").text("正在删除中！请稍等！");
			}
		});
	}
}
function delNews(nId){
	var flag=confirm("您确认要删除当前新闻吗");
	if(flag){
		$.ajax({
			"type":"post",
			"url":"../news/handleNews",
			"data":{
				"id":nId,
				"type":"delect"
			},
			dataType: "json",
			"success":function(data){
		    	if (data.success == "成功") {
		    		alert("删除成功！")
		    		$("#delectNewsTiShi").text("");
		    		window.location.reload();
		    	} else {
		    		$("#delectNewsTiShi").text("新闻删除失败...请重试或联系客服人员...").css({"color":"red"});
		    	}
			},
			"error":function(){
				$("#delectNewsTiShi").text("网络异常！请重试或联系管理员").css({"color":"red"});
			},
			"beforeSend":function(){
				$("#delectNewsTiShi").text("正在删除中！请稍等！");
			}
		});
	}
}